const Router = require("koa-router");
import Department from "../lib/models/Department";
import {
  responseResult,
  responseError,
  ERROR,
  responseOK,
} from "../lib/common/result";
import { any } from "sequelize/types/lib/operators";

const router = new Router();

console.log("-----begin users router--------");

router.get("/outapi/department/:id", async (ctx: any) => {
  let department = await Department.findByPk(ctx.request.params.id);
  if (!department) {
    responseError(ctx, ERROR.USERNAME, "用户不存在");
    return;
  }
  responseResult(ctx, department);
  return;
});

router.put("/outapi/department", async (ctx: any) => {
  if (!ctx.request.body || ctx.request.body.npwd === "") {
    responseError(ctx, ERROR.PARAM, "参数错误");
    return;
  }
  let dp = await Department.findByPk(ctx.request.body.un);
  if (!dp) {
    responseError(ctx, ERROR.USERNAME, "用户不存在");
    return;
  }
  dp.save();
  return;
});

router.delete("/outapi/department/:id", async (ctx: any) => {
  if (!ctx.body.params || !ctx.body.params.id) {
    responseError(ctx, ERROR.PARAM, "参数错误");
  } else {
    Department.destroy({
      where: { dId: ctx.body.params.id },
    })
      .then(() => {
        responseOK(ctx);
      })
      .catch((err: any) => {
        responseError(ctx, ERROR.USERNAME, err.toString());
      });
  }
  return;
});

module.exports = router;
